package com.cqnu.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.cqnu.common.ServiceException;


public class CommonUtil {

	public static String encoder(String source) {
		try {
			return URLEncoder.encode(source, "UTF-8");
		} catch (UnsupportedEncodingException e) {
		}
		return "";
	}

	public static String decoder(String source) {
		try {
			return URLDecoder.decode(source, "UTF-8");
		} catch (UnsupportedEncodingException e) {
		}
		return "";
	}

	/**
	 * 字符串转整型,如出现异常返�?
	 * 
	 * @param str
	 * @return
	 */
	public static int parseInt(String str) {
		return parseInt(str, 0);
	}

	/**
	 * 字符串转整型,�?��值不小于minVal
	 * 
	 * @param str
	 * @param minVal
	 * @return
	 */
	public static int parseIntMin(String str, int minVal) {
		int result = parseInt(str, 0);
		return result < minVal ? minVal : result;
	}

	/**
	 * 字符串转整型,�?��值不小于minVal,�?��值不超过maxVal
	 * 
	 * @param str
	 * @param minVal
	 * @param maxVal
	 * @return
	 */
	public static int parseInt(String str, int minVal, int maxVal) {
		int result = parseInt(str, minVal);
		if (result < minVal) {
			result = minVal;
		}
		return result > maxVal ? maxVal : result;
	}

	/**
	 * 字符串转整型,如出现异常返回defaultVal
	 * 
	 * @param str
	 * @param defaultVal
	 * @return
	 */
	public static int parseInt(String str, int defaultVal) {
		if (str != null && !"".equals(str)) {
			try {
				return Integer.parseInt(str);
			} catch (Exception e) {
			}
		}
		return defaultVal;
	}

	/**
	 * 字符串转double,如出现异常返�?
	 * 
	 * @param str
	 * @return
	 */
	public static double parseDouble(String str) {
		return parseDouble(str, 0.0);
	}

	/**
	 * 字符串转double,如出现异常返回defaultVal
	 * 
	 * @param str
	 * @param defaultVal
	 * @return
	 */
	public static double parseDouble(String str, double defaultVal) {
		if (str != null && !"".equals(str)) {
			try {
				return Double.parseDouble(str);
			} catch (Exception e) {
			}
		}
		return defaultVal;
	}

	public static void notEmpty(String str, String errMessage)
			throws ServiceException {
		if (StringUtils.isBlank(str) || "null".equalsIgnoreCase(str)) {
			exceptMsg(errMessage);
		}
	}

	public static void notEmpty(Object str, String errMessage)
			throws ServiceException {
		if (str == null) {
			exceptMsg(errMessage);
		}
	}

	public static void checkBoolean(boolean str, String errMessage)
			throws ServiceException {
		if (str) {
			exceptMsg(errMessage);
		}
	}

	public static void exceptMsg(String errMessage) throws ServiceException {
		throw new ServiceException(errMessage);
	}

	/**
	 * 判断查询结果集中是否存在某列
	 * 
	 * @param rs
	 * @param columnName
	 * @return
	 */
	public boolean isExistColumn(ResultSet rs, String columnName) {
		try {
			return rs.findColumn(columnName) > 0;
		} catch (SQLException e) {
		}
		return false;
	}

	/**
	 * 设置Cookie
	 * 
	 * @param response
	 * @param name
	 * @param value
	 * @param expiry
	 */
	public static void addCookie(HttpServletResponse response, String name,
			String value, int expiry) {
		Cookie cookie = new Cookie(name, encoder(value));
		cookie.setMaxAge(expiry);
		response.addCookie(cookie);
	}

	/**
	 * 读取Cookie�?
	 * 
	 * @param request
	 * @param name
	 * @return
	 */
	public static String getCookie(HttpServletRequest request, String name) {
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (Cookie c : cookies) {
				if (c.getName().equalsIgnoreCase(name)) {
					return decoder(c.getValue());
				}
			}
		}
		return "";
	}

	public static String getNowDate() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
	}

	public static String getNowDateWithOutTime() {
		return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
	}
	public static String formatMyDate(Date d) {
		if (null == d) {
			return "";
		}
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d);
	}

	public static int compare_date(String t1, String t2) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		try {
			c1.setTime(formatter.parse(t1));
			c2.setTime(formatter.parse(t2));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int result = c1.compareTo(c2);
		return result;
	}

	 
}
